package com.wy.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.wy.user.util.MD5Util;
import com.wy.user.util.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wy.user.entity.UserEntity;
import com.wy.user.service.UserService;
import com.wy.common.utils.PageUtils;
import com.wy.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author wangyu
 * @email sunlightcs@gmail.com
 * @date 2022-02-22 14:11:30
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private OssUtils ossUtils;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 注册
     */
    @PostMapping("/loginAdd")
    public R loginAdd (@RequestBody UserEntity userEntity) {
        return userService.loginAdd(userEntity);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login (@RequestBody UserEntity userEntity) {
        return userService.login(userEntity);
    }

    /**
     * oss 图片上传
     */

    @RequestMapping("/upload")
    public R photo (MultipartFile file) {
        R r = ossUtils.uploadFile(file);
        return r;
    }

    /**
     * 手机号不重复
     */
    @RequestMapping("/phone")
    public R phone (Long id) {
        UserEntity byId = userService.getById(id);
        if (byId==null) {
            return R.ok();
        }
        return R.error();
    }
}
